package cn.dzh.utils;


import lombok.extern.slf4j.Slf4j;

import static cn.dzh.utils.JwtUtil.*;

/**
 * ClassName: TokenUtil
 * Package: cn.dzh.utils
 */
@Slf4j
public class TokenUtil {
    public Boolean Before(String token,String requestURL) throws Exception{
        log.info("进入工具类，进行权限的授权！");
        //获取请求路径
        //获取Token
        String rolecode = parseJWTGetRoleCode(token);
        log.info("本次requestURL值为：{}",requestURL);
        log.info("本次携带Token的rolecode值为：{}",rolecode);
        /**
         * 数据回显
         */
        if (requestURL.equals("/goods/commodity/comebackdata")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"find::product::all") || checkValueInArray(rolecode,"find::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 编辑商品
         */
        if (requestURL.equals("/goods/commodity/update")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::one") || checkValueInArray(rolecode,"editor::draft::self")  || checkValueInArray(rolecode,"editor::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 商品加到草稿表中 addToDraft
         */
        if (requestURL.equals("/goods/commodity/addToDraft")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            // yinggshi editor::draft::self
            if (checkValueInArray(rolecode,"editor::draft::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 商品添加到商品表中 addToBase
         */
        if (requestURL.equals("/goods/operation/addToBase")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::one") || checkValueInArray(rolecode,"editor::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 上线商品 为确定权限
         */
        if (requestURL.equals("/goods/operation/goodsOnline")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::one") || checkValueInArray(rolecode,"editor::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 下线商品 为确定权限
         */
        if (requestURL.equals("/goods/operation/goodsOffline")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::one") || checkValueInArray(rolecode,"editor::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 推送审核 为确定权限
         */
        if (requestURL.equals("/goods/operation/pushAudit")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 审核通过 为确定权限
         */
        if (requestURL.equals("/goods/operation/approv")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::one")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("没有这个请求，继续往下查看");
        }
        /**
         * 审核驳回 为确定权限
         */
        if (requestURL.equals("/goods/operation/reject")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"editor::product::one")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("全部都没有这个请求");
        }
        /**
         * 商品查看 单个
         */
        if (requestURL.equals("/goods/commodity/selectBase")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"find::product::one") || checkValueInArray(rolecode,"find::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("全部都没有这个请求");
        }
        /**
         * 全局异常
         */
        if (requestURL.equals("/goods/error")){
            System.out.println("requestURL发请求过来了，进行鉴权");
            if (checkValueInArray(rolecode,"find::product::all")
                    || checkValueInArray(rolecode,"find::product::one")
                    || checkValueInArray(rolecode,"editor::product::one")
                    || checkValueInArray(rolecode,"find::product::self")
                    || checkValueInArray(rolecode,"find::draft::self")
                    || checkValueInArray(rolecode,"editor::product::self")){
                System.out.println("验证成功，执行后续");
                return true;
            } else {
                System.out.println("出错了");
            }
        }else{
            System.out.println("这个是全局异常");
            return false;
        }
        return false;
    }


    /**
     * 字符串转换为数组并检查数组中是否存在给定的参数值
     * @param str 原字符串
     * @param param 比较字符串
     * @return
     */
    public static boolean checkValueInArray(String str, String param) {
        // 去除字符串前后的方括号
        if (str.startsWith("[") && str.endsWith("]")) {
            str = str.substring(1, str.length() - 1);
        }

        String[] arr = str.split(", "); // 使用逗号和空格将字符串分割为数组
        for (String s : arr) {
            if (s.equals(param)) { // 检查数组中的每个值是否等于参数值
                return true;
            }
        }
        return false; // 如果没有找到匹配的值，返回false
    }
}
